//获取浏览器的query参数
function getUrlParams() {
  let href = location.href;
  let regExp = /.+\?(.+)/;
  let param = regExp.exec(href)[1];
  let paramArr = param.split("&");
  let query = {};
  paramArr.forEach((item) => {
    let itemArr = item.split("=");
    query[itemArr[0]] = decodeURI(itemArr[1]);
  });

  return query;
}
let query = getUrlParams(); //获取浏览器query参数
let productid = query.productid / 1;
console.log(query);
//请求产品详情信息
const getProductName = () =>
  ajax({
    url: "http://chst.vip:1234/api/getproduct",
    data: { productid: productid },
  });

getProductName().then((res) => {
  let productArr = res.result;
  //渲染顶部三级菜单
  let minName = productArr[0].productName.split(" ")[0];
  if (minName.length > 12) {
    minName = minName.substr(0, 12);
  }
  let minId = document.querySelector("#minId");
  minId.innerHTML = minName;
  let maxId = document.querySelector("#maxId");
  maxId.innerHTML = `${query.bigName}>`;
  maxId.onclick = function(e){
    e.preventDefault()
    history.back()
  }

  //渲染商品详情部分
  let ProductListHtml = `
  <div>
    <div class="pic">
      ${productArr[0].productImg}
    </div>
    <p>${productArr[0].productName}</p>
    <ul><span>当前最低 ￥6000</span>  <span>优选评论 8266条</span></ul>
  </div>
  ${productArr[0].bjShop}
  `
  let ProductListMain = document.querySelector('.main')
  ProductListMain.innerHTML = ProductListHtml

  return ajax({
    url: "http://chst.vip:1234/api/getproductcom",
    data: { productid: productid },
  });
}).then(res=>{
  //渲染评价部分
  let productPjHtml = '';
  [...res.result].forEach(item=>{
    productPjHtml +=`
        <li>
          <p><span>${item.comName}</span> <span>${item.comTime}</span></p>
          <div>${item.comContent}</div>
          <p>${item.comFrom}</p>
        </li>
    `
  })
  let ProductPj = document.querySelector('.main-pl>ul')
  ProductPj.innerHTML =  productPjHtml
})

// 点击回到顶部功能
let backTop = document.querySelector(".footer-main>div>a");
backTop.onclick = function (e) {
  e.preventDefault();
  let distans = document.documentElement.scrollTop;
  let distansControl = setInterval(function () {
    distans -= 10;
    document.documentElement.scrollTop = distans;
    if (distans <= 0) {
      clearInterval(distansControl);
    }
  }, 3);
};
